
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.PUB;
using Apps.Models.PUB;
using Apps.IBLL.PUB;
namespace Apps.BLL.PUB
{
	public partial class PUB_DEV_CAMERABLL: Virtual_PUB_DEV_CAMERABLL,IPUB_DEV_CAMERABLL
	{
        public List<V_PUB_DEV_CAMERAS> GetList(ref GridPager pager, Expression<Func<V_PUB_DEV_CAMERAS, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_PUB_DEV_CAMERAS> queryData = db.V_PUB_DEV_CAMERAS.AsQueryable();
                queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }

    }
	public class Virtual_PUB_DEV_CAMERABLL
	{
        [Dependency]
        public IPUB_DEV_CAMERARepository m_Rep { get; set; }

		public virtual List<PUB_DEV_CAMERAModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<PUB_DEV_CAMERA> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>  a.NAME.Contains(queryStr)
								|| a.USERNAME.Contains(queryStr)
								|| a.IP.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<PUB_DEV_CAMERAModel> GetPagerList(ref GridPager pager, Expression<Func<PUB_DEV_CAMERA, bool>> whereLambda )
        {

            IQueryable<PUB_DEV_CAMERA> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<PUB_DEV_CAMERAModel> GetList(Expression<Func<PUB_DEV_CAMERA, bool>> whereLambda )
        {

            IQueryable<PUB_DEV_CAMERA> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<PUB_DEV_CAMERAModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<PUB_DEV_CAMERAModel>();
		}

        public virtual List<PUB_DEV_CAMERAModel> CreateModelList(ref IQueryable<PUB_DEV_CAMERA> queryData)
        {

            List<PUB_DEV_CAMERAModel> modelList = (from r in queryData
                                              select new PUB_DEV_CAMERAModel
                                              {
													CAMERA_ID = r.CAMERA_ID,
													NODE_ID = r.NODE_ID,
													MEDIASERVER_ID = r.MEDIASERVER_ID,
													CHANNEL = r.CHANNEL,
													BRAND = r.BRAND,
													TYPE = r.TYPE,
													NAME = r.NAME,
													USERNAME = r.USERNAME,
													PASSWORD = r.PASSWORD,
													IP = r.IP,
													HTTP_PORT = r.HTTP_PORT,
													RTSP_PORT = r.RTSP_PORT,
													SERVER_PORT = r.SERVER_PORT,
													IS_GIS = r.IS_GIS,
													IS_VIEW = r.IS_VIEW,
													IS_ACCESS_TIGER = r.IS_ACCESS_TIGER,
													NET_STATUS = r.NET_STATUS,
													NET_STATUS_TIME = r.NET_STATUS_TIME,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, PUB_DEV_CAMERAModel model)
        {
            try
            {
                PUB_DEV_CAMERA entity = m_Rep.GetById(model.CAMERA_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new PUB_DEV_CAMERA();
               				entity.CAMERA_ID = model.CAMERA_ID;
				entity.NODE_ID = model.NODE_ID;
				entity.MEDIASERVER_ID = model.MEDIASERVER_ID;
				entity.CHANNEL = model.CHANNEL;
				entity.BRAND = model.BRAND;
				entity.TYPE = model.TYPE;
				entity.NAME = model.NAME;
				entity.USERNAME = model.USERNAME;
				entity.PASSWORD = model.PASSWORD;
				entity.IP = model.IP;
				entity.HTTP_PORT = model.HTTP_PORT;
				entity.RTSP_PORT = model.RTSP_PORT;
				entity.SERVER_PORT = model.SERVER_PORT;
				entity.IS_GIS = model.IS_GIS;
				entity.IS_VIEW = model.IS_VIEW;
				entity.IS_ACCESS_TIGER = model.IS_ACCESS_TIGER;
				entity.NET_STATUS = model.NET_STATUS;
				entity.NET_STATUS_TIME = model.NET_STATUS_TIME;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<PUB_DEV_CAMERA, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.PUB_DEV_CAMERA.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, PUB_DEV_CAMERAModel model)
        {
            try
            {
                PUB_DEV_CAMERA entity = m_Rep.GetById(model.CAMERA_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.CAMERA_ID = model.CAMERA_ID;
				entity.NODE_ID = model.NODE_ID;
				entity.MEDIASERVER_ID = model.MEDIASERVER_ID;
				entity.CHANNEL = model.CHANNEL;
				entity.BRAND = model.BRAND;
				entity.TYPE = model.TYPE;
				entity.NAME = model.NAME;
				entity.USERNAME = model.USERNAME;
				entity.PASSWORD = model.PASSWORD;
				entity.IP = model.IP;
				entity.HTTP_PORT = model.HTTP_PORT;
				entity.RTSP_PORT = model.RTSP_PORT;
				entity.SERVER_PORT = model.SERVER_PORT;
				entity.IS_GIS = model.IS_GIS;
				entity.IS_VIEW = model.IS_VIEW;
				entity.IS_ACCESS_TIGER = model.IS_ACCESS_TIGER;
				entity.NET_STATUS = model.NET_STATUS;
				entity.NET_STATUS_TIME = model.NET_STATUS_TIME;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual PUB_DEV_CAMERAModel GetById(object id)
        {
            if (IsExists(id))
            {
                PUB_DEV_CAMERA entity = m_Rep.GetById(id);
                PUB_DEV_CAMERAModel model = new PUB_DEV_CAMERAModel();
                              				model.CAMERA_ID = entity.CAMERA_ID;
				model.NODE_ID = entity.NODE_ID;
				model.MEDIASERVER_ID = entity.MEDIASERVER_ID;
				model.CHANNEL = entity.CHANNEL;
				model.BRAND = entity.BRAND;
				model.TYPE = entity.TYPE;
				model.NAME = entity.NAME;
				model.USERNAME = entity.USERNAME;
				model.PASSWORD = entity.PASSWORD;
				model.IP = entity.IP;
				model.HTTP_PORT = entity.HTTP_PORT;
				model.RTSP_PORT = entity.RTSP_PORT;
				model.SERVER_PORT = entity.SERVER_PORT;
				model.IS_GIS = entity.IS_GIS;
				model.IS_VIEW = entity.IS_VIEW;
				model.IS_ACCESS_TIGER = entity.IS_ACCESS_TIGER;
				model.NET_STATUS = entity.NET_STATUS;
				model.NET_STATUS_TIME = entity.NET_STATUS_TIME;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual PUB_DEV_CAMERAModel GetItem(Expression<Func<PUB_DEV_CAMERA, bool>> whereLambda)
        {
 
                PUB_DEV_CAMERA entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                PUB_DEV_CAMERAModel model = new PUB_DEV_CAMERAModel();
                              				model.CAMERA_ID = entity.CAMERA_ID;
				model.NODE_ID = entity.NODE_ID;
				model.MEDIASERVER_ID = entity.MEDIASERVER_ID;
				model.CHANNEL = entity.CHANNEL;
				model.BRAND = entity.BRAND;
				model.TYPE = entity.TYPE;
				model.NAME = entity.NAME;
				model.USERNAME = entity.USERNAME;
				model.PASSWORD = entity.PASSWORD;
				model.IP = entity.IP;
				model.HTTP_PORT = entity.HTTP_PORT;
				model.RTSP_PORT = entity.RTSP_PORT;
				model.SERVER_PORT = entity.SERVER_PORT;
				model.IS_GIS = entity.IS_GIS;
				model.IS_VIEW = entity.IS_VIEW;
				model.IS_ACCESS_TIGER = entity.IS_ACCESS_TIGER;
				model.NET_STATUS = entity.NET_STATUS;
				model.NET_STATUS_TIME = entity.NET_STATUS_TIME;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<PUB_DEV_CAMERAModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.CAMERA_ID, "CAMERA_ID");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.NODE_ID, "NODE_ID");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.MEDIASERVER_ID, "MEDIASERVER_ID");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.CHANNEL, "CHANNEL");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.BRAND, "BRAND");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.TYPE, "TYPE");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.NAME, "NAME");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.USERNAME, "USERNAME");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.PASSWORD, "PASSWORD");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.IP, "IP");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.HTTP_PORT, "HTTP_PORT");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.RTSP_PORT, "RTSP_PORT");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.SERVER_PORT, "SERVER_PORT");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.IS_GIS, "IS_GIS");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.IS_VIEW, "IS_VIEW");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.IS_ACCESS_TIGER, "IS_ACCESS_TIGER");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.NET_STATUS, "NET_STATUS");
				 excelFile.AddMapping<PUB_DEV_CAMERAModel>(x => x.NET_STATUS_TIME, "NET_STATUS_TIME");
 
            //SheetName
            var excelContent = excelFile.Worksheet<PUB_DEV_CAMERAModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new PUB_DEV_CAMERAModel();
						 				  entity.CAMERA_ID = row.CAMERA_ID;
				  entity.NODE_ID = row.NODE_ID;
				  entity.MEDIASERVER_ID = row.MEDIASERVER_ID;
				  entity.CHANNEL = row.CHANNEL;
				  entity.BRAND = row.BRAND;
				  entity.TYPE = row.TYPE;
				  entity.NAME = row.NAME;
				  entity.USERNAME = row.USERNAME;
				  entity.PASSWORD = row.PASSWORD;
				  entity.IP = row.IP;
				  entity.HTTP_PORT = row.HTTP_PORT;
				  entity.RTSP_PORT = row.RTSP_PORT;
				  entity.SERVER_PORT = row.SERVER_PORT;
				  entity.IS_GIS = row.IS_GIS;
				  entity.IS_VIEW = row.IS_VIEW;
				  entity.IS_ACCESS_TIGER = row.IS_ACCESS_TIGER;
				  entity.NET_STATUS = row.NET_STATUS;
				  entity.NET_STATUS_TIME = row.NET_STATUS_TIME;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<PUB_DEV_CAMERAModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        PUB_DEV_CAMERA entity = new PUB_DEV_CAMERA();
                       						entity.CAMERA_ID = model.CAMERA_ID;
						entity.NODE_ID = model.NODE_ID;
						entity.MEDIASERVER_ID = model.MEDIASERVER_ID;
						entity.CHANNEL = model.CHANNEL;
						entity.BRAND = model.BRAND;
						entity.TYPE = model.TYPE;
						entity.NAME = model.NAME;
						entity.USERNAME = model.USERNAME;
						entity.PASSWORD = model.PASSWORD;
						entity.IP = model.IP;
						entity.HTTP_PORT = model.HTTP_PORT;
						entity.RTSP_PORT = model.RTSP_PORT;
						entity.SERVER_PORT = model.SERVER_PORT;
						entity.IS_GIS = model.IS_GIS;
						entity.IS_VIEW = model.IS_VIEW;
						entity.IS_ACCESS_TIGER = model.IS_ACCESS_TIGER;
						entity.NET_STATUS = model.NET_STATUS;
						entity.NET_STATUS_TIME = model.NET_STATUS_TIME;
 
                        db.PUB_DEV_CAMERA.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
